//
//  MYComposeViewController.m
//  奇闻
//
//  Created by 似水灵修 on 14-11-30.
//  Copyright (c) 2014年 MingYueGuCheng. All rights reserved.
//

#import "MYComposeViewController.h"
#import "MYTextView.h"
#import "MYComposeBar.h"
#import "MYComposePhotosView.h"
#import "MYComposeTool.h"
#import "MBProgressHUD+MJ.h"

@interface MYComposeViewController ()<MYComposeBarDelegate, UITextViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (nonatomic, weak) MYTextView *textView;
@property (nonatomic, weak) MYComposeBar *composeBar;
@property (nonatomic, weak) MYComposePhotosView *photosView;
@end
@implementation MYComposeViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    //添加导航栏内容
    [self setupNavBar];
    // 添加textView
    [self setupTextView];
    // 添加工具条
    [self setupComposeBar];
    
    //添加通知
    // 只要系统的键盘一出现就会发送通知
    // UIKeyboardWillShowNotification监听键盘即将弹出的时候
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showKeyboard:) name:UIKeyboardWillShowNotification object:nil];
     // UIKeyboardWillHideNotification监听键盘即将隐藏的时候
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideKeyboard:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)showKeyboard:(NSNotification *)notification
{
    self.composeBar.transform = CGAffineTransformMakeTranslation(0, -[notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height);
}
- (void)hideKeyboard:(NSNotification *)notification
{
    self.composeBar.transform = CGAffineTransformIdentity;
}
// 添加工具条
- (void)setupComposeBar
{
    MYComposeBar *composeBar = [[MYComposeBar alloc] init];
    composeBar.delegate = self;
    CGFloat x = 0;
    CGFloat w = self.view.width;
    CGFloat h = 35;
    CGFloat y = self.view.height - h;
    composeBar.frame = CGRectMake(x, y, w, h);
    [self.view addSubview:composeBar];
    self.composeBar = composeBar;
}
 //添加导航栏内容
- (void)setupNavBar
{
    self.title = @"撰写奇闻";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStyleBordered target:self action:@selector(cancle)];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"发送" forState:UIControlStateNormal];
    // 设置正常状态下的颜色
    [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateDisabled];
    [btn addTarget:self action:@selector(send) forControlEvents:UIControlEventTouchUpInside];
    [btn sizeToFit];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.rightBarButtonItem.enabled = NO;
}
- (void)send
{
    if (_photosView.images.count) { // 发送图片
        // 注意奇闻接口并没有开放多张图片上传,只能上传一张
        [self composeImage];
    }else{ // 发送文字奇闻
        [self composeText];
    }
}
// 注意奇闻接口并没有开放多张图片上传,只能上传一张
- (void)composeImage
{
    UIImage *image = [self.photosView.images firstObject];
    NSString *status = self.textView.text.length ? self.textView.text : @"分享图片";
    [MBProgressHUD showMessage:@"正在发送奇闻...."];
    [MYComposeTool composeWithImage:image status:status success:^{
        [MBProgressHUD hideHUD];
        [MBProgressHUD showSuccess:@"发送成功"];
        [self cancle];
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUD];
        [self cancle];
        [MBProgressHUD showError:@"发送失败"];
    }];
    
}
// 发送文字奇闻
- (void)composeText
{
    [MBProgressHUD showMessage:@"正在发送奇闻...."];
    [MYComposeTool composeWithStatus:_textView.text success:^{
        [MBProgressHUD hideHUD];
        [MBProgressHUD showSuccess:@"发送成功"];
        [self cancle];
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUD];
        [self cancle];
        [MBProgressHUD showError:@"发送失败"];
    }];
}
- (void)cancle
{
    if ([self.delegate respondsToSelector:@selector(composeViewControllerDidCancle:)]) {
        [self.delegate composeViewControllerDidCancle:self];
    }
}
// 添加textView
- (void)setupTextView
{
    MYTextView *textView = [[MYTextView alloc] init];
    textView.placeholder = @"分享新鲜shi....";
    textView.frame = self.view.bounds;
    textView.alwaysBounceVertical = YES;
    textView.delegate = self;
    [self.view addSubview:textView];
    self.textView = textView;
    
   // 添加相册view
    MYComposePhotosView *photosView = [[MYComposePhotosView alloc] initWithFrame:self.view.bounds];
    photosView.y = 70;
    [self.textView addSubview:photosView];
    self.photosView = photosView;
}
- (void)composeBar:(MYComposeBar *)composeBar didClickBtn:(MYComposeBarButtonStyle)style
{
    switch (style) {
        case MYComposeBarButtonStylePicture: // 相册
        {
            // 弹出相册
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            // 相册数据源
            picker.delegate = self;
            picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
            [self presentViewController:picker animated:YES completion:nil];
        }
            break;
        case MYComposeBarButtonStyleMention: // 提及
        {
            // 弹出表情键盘
            
        }
            break;
        case MYComposeBarButtonStyleTrend: // 趋势
        {
        }
            break;
        case MYComposeBarButtonStyleEmoticon: // 表情
        {
            
        }
            break;
        case MYComposeBarButtonStyleKeyboard: // 键盘
        {
            [self.view endEditing:YES];
        }
            break;

        default:
            break;
    }
}
// 当在系统相册里选择完一张图片的时候就会调用
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = info[UIImagePickerControllerOriginalImage];
    [self.photosView addImage:image];
    self.navigationItem.rightBarButtonItem.enabled = YES;
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)textViewDidChange:(UITextView *)textView
{
    self.textView.hidePlaceHolder = self.textView.text.length;
    self.navigationItem.rightBarButtonItem.enabled = self.textView.text.length;
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.view endEditing:YES];
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self.textView becomeFirstResponder];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.textView endEditing:YES];
}
@end
